//package com.social.quaryz;
//
//
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.mail.SimpleMailMessage;
//import org.springframework.mail.javamail.JavaMailSender;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//
//
///**
// * @author zero
// * @version 1.0
// * @project demo
// * @description 定时任务
// * @date 2022/10/23 20:31:35
// */
//@Component
//public class Task {
//    @Value("${spring.mail.username}")
//    private String username;
//    @Resource
//    private JavaMailSender mailSender;
//
//    @Scheduled(cron = "0/60 * * * * ? ")
//    public void deletePetiton(){
//        //邮件接收人的email地址
//        String recipientEmailAddr = "2020110343@stu.sicnu.edu.cn";
////        List<User> userList = userService.queryAllUsers();
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(username);
//        message.setTo(recipientEmailAddr);
//        message.setSubject("主题:用户数据");
//        message.setText("测试邮件内容");
//        mailSender.sendFireMessage(message);
//    }
//}
package com.social.quaryz;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.social.config.netty.MyChannelHandlerPool;
import com.social.pojo.ActivityRank;
import com.social.pojo.Petition;
import com.social.service.ActivityRankService;
import com.social.service.PetitionService;
import com.social.utils.RankData;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.social.config.netty.MyWebSocketHandler.CHANNEL_SET_KEY;
import static com.social.constant.RedisConstants.*;


/**
 * @author zero
 * @version 1.0
 * @project demo
 * @description 定时任务
 * @date 2022/10/23 20:31:35
 */
@Component
@Slf4j
public class Task {
//    @Resource
//    private FirecheckService firecheckService;
    @Resource
    private PetitionService petitionService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private ActivityRankService activityRankService;
    //每个月15日10:15分执行
    @Scheduled(cron = "0 15 10 15 * ?")
    public void sendFireMessage(){
        System.out.println("该检查消防设施啦");
    }

    @Scheduled(cron = "0 0 18 * * ? " )
    public void sendPetiton(){
        List<Petition> todayPetition = petitionService.getTodayPetition();
        log.info("发送信访到各个地方");
    }
    //每30min一次
    @Scheduled(cron = "0 0/10 0 * * ? ")
    public void clearChannel(){
        log.info("清除非法连接");
        stringRedisTemplate.opsForZSet().removeRangeByScore(CHANNEL_SET_KEY,-1,System.currentTimeMillis() - 30 * 60 * 60);

        for (Channel channel : MyChannelHandlerPool.channelGroup) {
            Boolean isSuccess = stringRedisTemplate.opsForSet().isMember(CHANNEL_SET_KEY, JSONUtil.toJsonStr(channel));
            if (!isSuccess){
                MyChannelHandlerPool.channelGroup.remove(channel);
            }
        }
    }

    @Scheduled(cron = "0 0/30 0 * * ? ")
    public void clearRankChannel(){
        log.info("清除过期活动");
        Set<String> json = stringRedisTemplate.opsForZSet().rangeByScore(ACTIVITY_ALL_SET, 0, System.currentTimeMillis());
        stringRedisTemplate.opsForZSet().remove(ACTIVITY_ALL_SET,json.toArray(new String[]{}));

        List<ActivityRank> rank = json.stream()
                .map(idx -> operate(idx))
                .collect(Collectors.toList());
        activityRankService.saveOrUpdateBatch(rank);
        stringRedisTemplate.opsForZSet().remove(ACTIVITY_RANK_SET,json.toArray(new String[]{}));

    }

    private static final Byte OVER_FINISH = 1;
    private ActivityRank operate(String idx) {
        Long id = BeanUtil.toBean(idx, RankData.class).getId();
        ActivityRank rank = new ActivityRank();
        rank.setId(id);
        rank.setIs_over(OVER_FINISH);
        Long score = stringRedisTemplate.opsForZSet().score(ACTIVITY_RANK_SET, idx).longValue();
        rank.setReading_num(score);
        stringRedisTemplate.delete(ACTIVITY_ID_KEY + id);
        return rank;
    }

}

